<?php
/**
 * 系统装修材料管理模块
 *
 * @author river_he@eueang.com
 */
final class Kit_Model_Decorate extends Kit_Model_Abstract{
	/**
	 * 获取材料列表
	 * 
	 * @param array|string $where
	 * @param string $sort
	 * @param int $page
	 * @param int $psize
	 * @return array('data'=>array(),'pager'=>array())
	 */
	public function getItems($where, $sort='', $page=1, $psize=10){
		$return = array();
		$data = $this->getDecorateDao()->getItems($where, $sort, $page, $psize);
		if($data){
			$page = $page>0?$page:1;
			$psize = $psize>0?$psize:10;
			$return['data'] = $data;
			$total = $this->getDecorateDao()->getWhereTotal($where);
			$return['pager'] = array(
			'rtotal'=>$total,
			'page'=>$page,
			'psize'=>$psize,
			'ptotal'=>ceil($total/$psize)
			);
		}
		return $return;
	}
	
	/**
	 * 出售家具
	 * 
	 * @param int $shoperId
	 * @param int $shoperDecoId
	 * @return int 1:OK -1:no decorate -2:no shoper -3:already used -4:no shoper -5:operation error -6:server error
	 */
	public function sell($shoperId, $shoperDecoId){
		$shoperDeco = $this->getShoperDecoDao()->getItem('id='.$shoperDecoId);
		if(!$shoperDeco){
			return -1;
		}elseif($shoperDeco['nUid'] != $shoperId){
			return -2;
		}elseif(1 != $shoperDeco['nStatus']){
			return -3;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shoperId);
		if(!$shoper){
			return -4;
		}
		$pay = $shoperDeco['nPrice'];
		$set = array();
		if(0 < $pay){
			if(1 == $shoperDeco['nPriceType']){
				$pay += $shoper['nGold'];
				$set['nGold'] = $pay;
			}elseif(2 == $shoperDeco['nPriceType']){
				$pay += $shoper['nKb'];
				$set['nKb'] = $pay;
			}
		}
		if($set){
			if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
				return -5;
			}
		}
		if($this->getShoperDecoDao()->update('id='.$shoperDeco['id'], array('nStatus'=>4))){
			return 1;
		}
		return -6;
	}
	
	/**
	 * 购买装修家具
	 * 
	 * @param int $shoperId
	 * @param int $decoId
	 * @param int $total
	 * @return int|array -1.no decorate -2.no shoper -3.price error -4.no enough gold -5.no enough kb
	 */
	public function buy($shoperId, $decoId, $total){
		$deorate = $this->getDecorateDao()->getItem('id='.$decoId);
		if(!$deorate){
			return -1;
		}
		$shoper = $this->getShoperDao()->getItem('id='.$shoperId);
		if(!$shoper){
			return -2; 
		}
		$pay = 1>$deorate['nRebate']?ceil($deorate['nRebate']*$deorate['nPrice']*$total):$deorate['nPrice']*$total;
		if(0 > $pay){
			return -3;
		}elseif(0 < $pay){
			$set = array();
			if(1 == $deorate['nPriceType']){
				if($shoper['nGold'] < $pay){
					return -4;
				}else{
					$set['nGold'] = $shoper['nGold']-$pay;
				}
			}else if(2 == $deorate['nPriceType']){
				if($shoper['nKb'] < $pay){
					return -5;
				}else{
					$set['nKb'] = $shoper['nKb']-$pay;
				}
			}
			if($set){
				if(!$this->getShoperDao()->update('id='.$shoper['id'], $set)){
					return -6;
				}
			}
		}
		$hasWhere = array('nUid='.$shoper['id']);
		array_push($hasWhere, 'nDid='.$deorate['id']);
		$hasDeco = $this->getShoperDecoDao()->getItem($hasWhere);
		if($hasDeco){
			$hasDeco['nTotal'] += $total;
			$this->getShoperDecoDao()->update('id='.$hasDeco['id'],  array('nTotal'=>$hasDeco['nTotal']));
			$this->getDecorateDao()->update('id='.$deorate['id'], array('nSells'=>$deorate['nSells']+$total));
			return $hasDeco;
		}else{
			$record = array(
			'nUid'=>$shoper['id'],
			'nDid'=>$deorate['id'],
			'sName'=>$deorate['sName'],
			'nTotal'=>$total,
			'nCrTime'=>Kit_Util::getTimeStamp()
			);
			$id = $this->getShoperDecoDao()->add($record);
			$record['id'] = $id;
			return $record;
		}
		return -7;
	}
}
//END